import { PropertyUtil } from '@/components/Attribute/comps/PropertyUtil';
import { getImgPath } from '@/util/common';
import { GraphShapeUtil } from './GraphShapeUtil';
import { behaviors } from 'src/pages/mxgraph/behaviors';
import MergeNodeBehavior from "@/pages/mxgraph/behaviors/ActivityDiagram/MergeNodeBehavior";

export class PopMenuUtil {
  static getPopMenu(shape: UmlShape, diagramType: string) {
    //// const typeName = GraphShapeUtil.parseStName(shape.getTypeName());
    const typeName = shape.getTypeName();
    //// console.log('typeName', typeName, diagramType);
    const items =
      app.activeProject.configManager.getPopMenuConfig()[diagramType]?.[
        typeName
      ] || [];
    const typeNameConfig = app.activeProject.configManager.getTypeNameConfig();
    //// console.log('typeNameConfig', items, typeNameConfig);
    let menuItems = items.map(i => {
      if (i === 'SetType') {
        const typeProperty = PropertyUtil.getNodePropertyValue(shape.graphNode, 'Type');
        return {
          label: '类型指定',
          value: 'specifyType',
          iconPath: getImgPath(typeProperty ? 'ChangeBehavior' : 'SpecifyBehavior'),
          type: 'setType'
        };
      } else if (i === 'SetClassifier') {
        const classifiers = PropertyUtil.getNodePropertyValue(shape.graphNode, 'Classifier');
        return {
          label: '类型指定',
          value: 'setClassifier',
          iconPath: getImgPath(classifiers.length > 0 ? 'ChangeBehavior' : 'SpecifyBehavior'),
          type: 'setType'
        };
      } else if (i === 'ShowConstraintParameter') {
        return {
          label: '显示参数',
          value: 'showConstraintParameter',
          iconPath: 'statics/images/sysml/showConstraintParameter.png',
          type: 'port'
        };
      } else {
        const config = typeNameConfig[i];
        if (!config) return;
        return {
          label: config.cnName,
          value: config.typeName,
          iconPath: config.icon, ////  getImgPath(config.typeName),
          type: config.type
        };
      }
    }).filter(Boolean);
    //// console.log('menuItems', menuItems);
    const limitOutConnectorType = ['MergeNode', 'JoinNodeHorizontal', 'JoinNodeVertical'];
    if (limitOutConnectorType.includes(shape.getTypeName())) {
      const edgeTypes = ['ControlFlow', 'ObjectFlow'];
      const limitOutConnectors = (behaviors.get(shape.getTypeName()) as MergeNodeBehavior).limitOutConnectors('ControlFlow', shape);
      if (!limitOutConnectors) {
        menuItems = menuItems.filter(item => !edgeTypes.includes(item.value));
      }
    }
    return menuItems;
  }
}

